package com.japstudy.controller;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.japstudy.entity.Question;
import com.japstudy.entity.TestPaper;
import com.japstudy.entity.TestPaperQuestion;
import com.japstudy.entity.User;
import com.japstudy.entity.WrongWord;
import com.japstudy.service.IQuestionService;
import com.japstudy.service.ITestPaperQuestionService;
import com.japstudy.service.ITestPaperService;
import com.japstudy.service.IWrongWordService;
import com.japstudy.util.PageUtil;
import com.japstudy.util.UUIDUtil;

@Controller
@RequestMapping("/TestPaper")
public class TestPaperController {
	
	@Autowired
	private ITestPaperService testPaperServiceImpl;
	@Autowired
	private IQuestionService questionServiceImpl;
	@Autowired
	private ITestPaperQuestionService testPaperQuestionServiceImpl;
	@Autowired
	private IWrongWordService wrongWordServiceImpl;
	
	private void makeQuestion(HttpServletResponse response, HttpServletRequest request, String wordChinese, String wordPing, String wordPian, String wordWord) {
		String questionSex = (String) request.getSession().getAttribute("testSex");
		Question nowQuestion = (Question) request.getSession().getAttribute("nowPageQuestion");
		boolean flag = true;
		String yourAnswer = "";
		if(questionSex.equals("chineseToJapanese")) {
			if(nowQuestion.getWord().getWordPing() != null && !nowQuestion.getWord().getWordPing().equals("")) {
				if(!nowQuestion.getWord().getWordPing().equals(wordPing)) {
					flag = false;
				}
				yourAnswer += "wordPing-" + wordPing + ";";
			}
			if(nowQuestion.getWord().getWordPian() != null && !nowQuestion.getWord().getWordPian().equals("")) {
				if(!nowQuestion.getWord().getWordPian().equals(wordPian)) {
					flag = false;
				}
				yourAnswer += "wordPian-" + wordPian + ";";
			}
			if(nowQuestion.getWord().getWordWord() != null && !nowQuestion.getWord().getWordWord().equals("")) {
				if(!nowQuestion.getWord().getWordWord().equals(wordWord)) {
					flag = false;
				}
				yourAnswer += "wordWord-" + wordWord;
			}
		} else {
			if(-1 == nowQuestion.getWord().getWordChinese().indexOf(wordChinese)) {
				flag = false;
			}
			yourAnswer += "wordChinese-" + wordChinese;
		}
		if(!flag) {
			nowQuestion.setResult(0);
		} else {
			nowQuestion.setResult(1);
		}
		nowQuestion.setYourAnswer(yourAnswer);
		nowQuestion.setQuestionSex(questionSex);
	}
	
	@RequestMapping("prevQuestion")
	public String prevQuestion(HttpServletResponse response, HttpServletRequest request, String wordChinese, String wordPing, String wordPian, String wordWord) throws IOException {
		makeQuestion(response, request, wordChinese, wordPing, wordPian, wordWord);
		Integer nowQuestionIndex = (Integer) request.getSession().getAttribute("nowQuestionIndex");
		List<Question> realTestPaperQuestions = (List<Question>) request.getSession().getAttribute("realTestPaperQuestions");
		request.getSession().setAttribute("nowQuestionIndex", nowQuestionIndex-1);
		request.getSession().setAttribute("nowPageQuestion", realTestPaperQuestions.get(nowQuestionIndex-1-1));
		String questionSex = (String) request.getSession().getAttribute("testSex");
		if(questionSex.equals("chineseToJapanese")) {
			response.sendRedirect(request.getContextPath() + "/Word/userTestPaperChineseUi");
		} else {
			response.sendRedirect(request.getContextPath() + "/Word/userTestPaperJapaneseUi");
		}
		return null;
	}
	
	@RequestMapping("nextQuestion")
	public String nextQuestion(HttpServletResponse response, HttpServletRequest request, String wordChinese, String wordPing, String wordPian, String wordWord) throws IOException {
		makeQuestion(response, request, wordChinese, wordPing, wordPian, wordWord);
		Integer nowQuestionIndex = (Integer) request.getSession().getAttribute("nowQuestionIndex");
		List<Question> realTestPaperQuestions = (List<Question>) request.getSession().getAttribute("realTestPaperQuestions");
		request.getSession().setAttribute("nowQuestionIndex", nowQuestionIndex+1);
		request.getSession().setAttribute("nowPageQuestion", realTestPaperQuestions.get(nowQuestionIndex));
		String questionSex = (String) request.getSession().getAttribute("testSex");
		if(questionSex.equals("chineseToJapanese")) {
			response.sendRedirect(request.getContextPath() + "/Word/userTestPaperChineseUi");
		} else {
			response.sendRedirect(request.getContextPath() + "/Word/userTestPaperJapaneseUi");
		}
		return null;
	}
	
	@RequestMapping("getResult")
	public String getResult(HttpServletResponse response, HttpServletRequest request, String wordChinese, String wordPing, String wordPian, String wordWord) throws IOException {
		makeQuestion(response, request, wordChinese, wordPing, wordPian, wordWord);
		List<Question> realTestPaperQuestions = (List<Question>) request.getSession().getAttribute("realTestPaperQuestions");
		User loginUser = (User) request.getSession().getAttribute("loginUser");
		java.util.Date nowDate = new java.util.Date();
		java.sql.Date testDate = new java.sql.Date(nowDate.getTime());
		TestPaper testPaper = new TestPaper(UUIDUtil.getUUID(), loginUser, testDate);
		testPaperServiceImpl.add(testPaper);
		List<Question> wrongQuestion = new ArrayList<Question>();
		for (Question question : realTestPaperQuestions) {
			questionServiceImpl.add(question);
			TestPaperQuestion testPaperQuestion = new TestPaperQuestion(testPaper, question);
			testPaperQuestionServiceImpl.add(testPaperQuestion);
			if(question.getResult() == 0) {
				wrongQuestion.add(question);
				WrongWord wrongWord = new WrongWord(loginUser, question.getWord(), 1, 0);
				wrongWordServiceImpl.insert(wrongWord);
			}
		}
		request.getSession().setAttribute("wrongQuestionCount", wrongQuestion.size());
		request.getSession().setAttribute("thisTestQuestion", wrongQuestion);
		return "user/get_result";
	}

	@RequestMapping("/selectAll")
	public String selectAll(HttpServletRequest request, HttpServletResponse response, String userId) throws IOException {
		List<TestPaper> testPapers = testPaperServiceImpl.selectAllByLoginUser(userId);
		List<TestPaper> pageData = PageUtil.getPageData(testPapers, 1, 10);
		request.getSession().setAttribute("realTestPapersUser", testPapers);
		request.getSession().setAttribute("testPapersUser", pageData);
		response.sendRedirect(request.getContextPath() + "/TestPaper/testPaperListUi");
		return null;
	}
	
	@RequestMapping("/testPaperListUi")
	public String testPaperListUi() {
		return "user/test_paper_list";
	}
	
	@RequestMapping("/pageCountChange")
	public @ResponseBody List<TestPaper> pageCountChange(HttpServletRequest request, HttpServletResponse response, String pageCount, String nowPageNumber) {
		List<TestPaper> testPapers = (List<TestPaper>) request.getSession().getAttribute("realTestPapersUser");
		int intPageCount = Integer.parseInt(pageCount);
		int intNowPageNumber = Integer.parseInt(nowPageNumber);
		int maxPage = PageUtil.getMaxPage(testPapers, intPageCount);
		if(intNowPageNumber > maxPage) {
			intNowPageNumber = maxPage;
		}
		List<TestPaper> pageData = PageUtil.getPageData(testPapers, intNowPageNumber, intPageCount);
		return pageData;
	}
	
	@RequestMapping("/toPage")
	public @ResponseBody List<TestPaper> toPage(HttpServletRequest request, HttpServletResponse response, String pageCount, String toPageNumber) {
		List<TestPaper> testPapers = (List<TestPaper>) request.getSession().getAttribute("realTestPapersUser");
		int intPageCount = Integer.parseInt(pageCount);
		int intToPageNumber = Integer.parseInt(toPageNumber);
		List<TestPaper> pageData = PageUtil.getPageData(testPapers, intToPageNumber, intPageCount);
		return pageData;
	}
	
	@RequestMapping("/selectQuestionByTestPaperId")
	public  String selectQuestionByTestPaperId(HttpServletRequest request, HttpServletResponse response, String testPaperId) throws IOException {
		response.sendRedirect(request.getContextPath() + "/Question/selectAllQuestionByTestPaperId?testPaperId=" + testPaperId);
		return null;
	}
}
